<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('类对象信息列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>表名称：</label>
                                <input type="text" name="tableName"/>
                            </li>
                            <li>
                                <label>英文名称：</label>
                                <input type="text" name="tableEnName"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.addTab()" shiro:hasPermission="table:table:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.editTab()" shiro:hasPermission="table:table:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
<!--                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="table:table:remove">-->
<!--                    <i class="fa fa-remove"></i> 删除-->
<!--                </a>-->
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="table:table:export">
                    <i class="fa fa-download"></i> 导出
                </a>
                <a class="btn btn-primary" onclick="createData()" shiro:hasPermission="table:table:createData">
                    <i class="fa fa-edit"></i> 一键生成数据
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
         <div class="modal fade" id="addTableInfo" tabindex="-1" role="dialog" aria-hidden="true">
             <input type="hidden" name="preProcessBranchId"/>
             <div class="modal-dialog modal-lg">
                 <div class="modal-content">
                     <div class="modal-header">
                         <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span
                                 class="sr-only">Close</span>
                         </button>
                         <h4 class="modal-title pull-left">一键生成数据</h4>
                     </div>
                     <div class="modal-body">
                         <div class="row">
                         <form class="form-horizontal" id="createForm">
                         </form>
                         </div>
                     </div>
                     <div class="modal-footer">
                         <button type="button" class="btn btn-white" data-dismiss="modal">取消</button>
                         <button type="button" class="btn btn-success" onclick="saveTableCreateInfo()">确定</button>
                     </div>
                 </div>
             </div>
         </div>
    </div>
    <th:block th:include="include :: footer" />
     <script id="tableListTpl" type="text/template">
         <input type="hidden" name="tableSize" value="{{d.ids.length}}">
         {{# layui.each(d.ids, function(index, a){ }}
         <input type="hidden" name="tableEnName{{index}}" value="{{d.tableEnNameRows[index]}}">
         <input type="hidden" name="tableId{{index}}" value="{{a}}">
         <div class="form-group col-sm-12">
             <label class="col-sm-5 control-label">{{d.tableNameRows[index]}}：</label>
             <div class="col-sm-2" style="line-height: 31px">
                 是否清空数据&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" class="form-check-input" name="clearTable{{index}}">
             </div>
             <div class="col-sm-4">
                 <input name="count{{index}}" class="form-control" type="number" required placeholder="数据增加条数">
             </div>
         </div>
         {{# }); }}
     </script>
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('table:table:edit')}]];
        var removeFlag = [[${@permission.hasPermi('table:table:remove')}]];
        var prefix = ctx + "table/table";
        var statusDatas = [[${@dict.getType('status')}]];
        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "类对象信息",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: 'ID',
                    visible: false
                },
                {
                    field: 'tableName',
                    title: '表名称',
                    width: 100
                },
                {
                    field: 'tableEnName',
                    title: '表英文名称',
                    width: 100
                },
                {
                    field: 'tableDescription',
                    title: '表说明',
                    width: 300
                },
                {
                    field: 'status',
                    title: '状态',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(statusDatas, value);
                    },
                    width: 50
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="detail(\'' + row.id + '\')"><i class="fa fa-search"></i>详情</a> ');
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.editTab(\'' + row.id + '\','+ ($(window).width() - 50) +')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="tabObjInfo(\'' + row.id + '\',\''+  row.tableEnName +'\',\''+  row.tableName +'\')"><i class="fa fa-edit"></i>查数据</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="indexList(\'' + row.id + '\')"><i class="fa fa-search"></i>索引列表</a>');
                        return actions.join('');
                    },
                    width: 200
                }]
            };
            $.table.init(options);
        });

        function detail(id) {
            $.operate.addTab2("表详情", ctx + "interface/interface/table_detail/" + id);
        }
        function indexList(id) {
            $.operate.addTab2("索引列表", ctx + "tableIndex/tableIndex/" + id);
        }
        //表数据列表
        function tabObjInfo(id,tableEnName,tableName) {
            $.operate.addTab2(tableName, prefix + "/tabList/" + id + "?name=" + encodeURIComponent(tableName)+"&enname=" + encodeURIComponent(tableEnName));
        }
        // 一键生成数据
        function createData(){
            var rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
            if (rows.length == 0) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            var tableEnNameRows = $.table.selectColumns("tableEnName");
            var tableNameRows = $.table.selectColumns("tableName");
            var tableListTplHtml = $("#tableListTpl").html();
            laytpl(tableListTplHtml).render({ids:rows,tableEnNameRows:tableEnNameRows,tableNameRows:tableNameRows}, function (html) {
                $("#createForm").html('')
                $("#createForm").html(html);
            });
            $('#addTableInfo').modal('show');
        }
        function saveTableCreateInfo() {
            var tableList = [];
            for(let i=0;i<$("input[name='tableSize']").val();i++){
                if($("input[name='count"+i+"']").val()=="" || $("input[name='count"+i+"']").val()==0){
                    $.modal.alertWarning("请输入正确的条数");
                    return false;
                }
                tableList.push(Object.assign({},
                    {
                        clearTable:$("input[name='clearTable"+i+"']").prop("checked"),
                        count:$("input[name='count"+i+"']").val(),
                        tableId:$("input[name='tableId"+i+"']").val(),
                        tableName:$("input[name='tableEnName"+i+"']").val()
                    })
                )
            }
            let config = {
                url: "http://code.suzhi.link:8070/dataManager/createMockData",
                type: "post",
                dataType: "json",
                contentType:"application/json",
                async:false,
                data: JSON.stringify(tableList),
                beforeSend: function () {
                    $.modal.loading("正在处理中，请稍后...");
                },
                success: function (result) {
                    $.modal.closeLoading();
                    if (result.errCode == 1) {
                        $.modal.alertSuccess("数据生成成功")
                        $('#addTableInfo').modal('hide')
                        $.table.refresh();
                        return false;
                    }
                    if (result.code == web_status.WARNING) {
                        $.modal.alertWarning(result.message)
                        return false;
                    }
                    $.modal.alertError(result.message);
                }
            };
            $.ajax(config);
        }
    </script>
</body>
</html>